﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using UFSSWebApp.UFSSservicereference;

namespace UFSSWebApp
{
    public partial class usersubs : System.Web.UI.Page
    {
        UFSSservicereference.UserCreds uc = new UFSSservicereference.UserCreds();
        UFSSservicereference.UFSSserviceSoapClient ussc = new UFSSservicereference.UFSSserviceSoapClient();
        private UFSSservicereference.UFSSFile[] filearray;
        private UFSSservicereference.UFSSSubscription sub;
        private ArrayList files = new ArrayList();

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                uc = (UFSSservicereference.UserCreds)Session["usercreds"];
                UFSSSubscription temp;
                if (Request.QueryString["id"] != null)
                {
                    temp = ussc.getSubInfoByID(uc, Int32.Parse(Request.QueryString["id"]));
                    Session.Remove("sub");
                    Session.Add("sub", temp.subName);
                }
                if (ussc.isSubbed(uc, Session["sub"].ToString()))
                    btnJoin.Text = "Leave Subscription";
                else
                    btnJoin.Text = "Join Subscripiton";

                try
                {
                    
                    sub = ussc.getSubInfo(Session["sub"].ToString());
                    filearray = ussc.getSubFiles(Session["sub"].ToString());
                    Session.Remove("subcreator");
                    Session.Remove("latfile");
                    Session.Add("subcreator", sub.ownername.ToString());
                    Session.Add("latfile", sub.latfile.ToString());
                }
                catch
                {
                    lbtnFile.Enabled = false;
                }

                if (!IsPostBack)
                    refresh();
            }
            catch
            {
                Response.Redirect("Error.aspx");
                
            }
        }

        private void refresh()
        {
            lstFiles.Enabled = true;
            lstSubs.Enabled = true;

            lstFiles.Items.Clear();
            lstSubs.Items.Clear();

            files.Clear();

            lblFiles.Text = sub.files.ToString();
            lblType.Text = sub.type.ToString();
            lblSubs.Text = sub.members.ToString();

            if (!sub.lastup.Equals(DateTime.MinValue))
                lblLastUp.Text = sub.lastup.ToString();
            else
                lblLastUp.Text = "No updates.";

            try
            {
                if (!sub.latfile.Equals(null))
                    lbtnFile.Enabled = true;
            }
            catch
            {
                lbtnFile.Enabled = false;
            }
                
            //files
            foreach (UFSSservicereference.UFSSFile file in filearray)
            {
                files.Add(file);
                lstFiles.Items.Add(file.filename.ToString());
            }

            if (lstFiles.Items.Count == 0)
            {
                lstFiles.Items.Add("No Files");
                lstFiles.Enabled = false;
            }

            //subscribers
            ArrayOfString members = new ArrayOfString();
            members = ussc.getSubMembers(Session["sub"].ToString());

            for (int i = 0; i < members.Count; i++)
                lstSubs.Items.Add(members[i].ToString());

        }

        protected void lbtnCreator_Click(object sender, EventArgs e)
        {
            Session.Remove("user");
            Session.Add("user", Session["subcreator"].ToString());
            Response.Redirect("~/userpage.aspx");
        }

        protected void btnViewProfile_Click(object sender, EventArgs e)
        {
            if (lstSubs.SelectedIndex != -1)
            {
                Session.Remove("user");
                Session.Add("user", lstSubs.SelectedItem.ToString());
                Response.Redirect("~/userpage.aspx");
            }
        }

        protected void btnViewFile_Click(object sender, EventArgs e)
        {
            if (lstFiles.SelectedIndex != -1)
            {
                Session.Remove("fileinfo");
                Session.Add("fileinfo", filearray[lstFiles.SelectedIndex]);
                Response.Redirect("~/userfile.aspx");
            }
        }

        protected void btnJoin_Click(object sender, EventArgs e)
        {
            if (btnJoin.Text.Equals("Join Subscripiton"))
            {
                try
                {
                    ussc.joinSubscriptin(uc, Session["sub"].ToString());
                }
                catch
                {
                }
            }
            else
            {
                try
                {
                    ussc.leaveSubscription(uc, Session["sub"].ToString());
                }
                catch
                {
                }
            }
        }

        protected void lbtnFile_Click(object sender, EventArgs e)
        {
            Session.Remove("fileinfo");
            Session.Add("fileinfo", filearray[lstFiles.Items.Count-1]);
            Response.Redirect("~/userfile.aspx");
        }
    }
}